import java.io.*;
import java.util.*;

public class Combiner
{
	static String s[];

    static int part(int l, int r)
    {
       int i,j;
       String v,b;
       v = s[r];
       i = l - 1;
       j = r;
       do
       {
         do
         {
           j--;
         }
         while ((s[j].compareTo(v) > 0) && (j != (i + 1)));

         do
         {
           i++;
         }
         while ((s[i].compareTo(v) < 0) && (i != (j - 1)));
         b = s[i];
         s[i] = s[j];
         s[j] = b;
       }
       while (i < j);
       s[j] = s[i];
       s[i] = s[r];
       s[r] = b;
       return i;
    }

    static void sort(int l,int t)
    {
       int i;
       if (l < t) 
       {
         i = part(l,t);
         sort(l,i-1);
         sort(i+1,t);
       }
    }

	public static void main(String args[]) throws Exception
	{
		if (args.length != 2)
		{
			System.out.println("Usage: Combiner <source dir> <destination file>");
			return;
		}
		DataOutputStream out = new DataOutputStream(new FileOutputStream(args[1]));
		MapFilter filter = new MapFilter("map");
		File dir = new File(args[0]);
		s = dir.list(filter);
		sort(0,s.length - 1);
		for (int i = 0; i < s.length; i++)
		{
			File file = new File(dir,s[i]);
			short size = (short)(file.length());
			out.writeShort(size);
			FileInputStream in = new FileInputStream(file);
			int c;
			while ((c = in.read()) != -1) out.write(c);
			in.close();
			System.out.println(s[i] + " " + size);
		}
		out.close();
	}
}